package com.ygqh.baby.service.impl;

import com.ygqh.baby.ao.DataStatus;
import com.ygqh.baby.ao.QueryInfo;
import com.ygqh.baby.ao.ResultSet;
import com.ygqh.baby.mapper.BiSkuSaleMapper;
import com.ygqh.baby.model.YgHotProductModel;
import com.ygqh.baby.po.BiSkuSale;
import com.ygqh.baby.po.BiSkuSaleExample;
import com.ygqh.baby.service.BiSkuSaleService;
import com.ygqh.baby.service.WdtQybOrderService;
import com.ygqh.baby.utils.DateConvertUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

@Service
public class BiSkuSaleServiceImpl implements BiSkuSaleService {

    @Autowired
    private BiSkuSaleMapper biSkuSaleMapper;
    @Autowired
    private WdtQybOrderService wdtQybOrderService;

    @Override
    public List<BiSkuSale> find() {
        BiSkuSaleExample example = new BiSkuSaleExample();
        example.createCriteria().andStatusEqualTo(DataStatus.Valid.name());
        return biSkuSaleMapper.selectByExample(example);
    }

    @Override
    public int findCount() {
        BiSkuSaleExample example = new BiSkuSaleExample();
        example.createCriteria().andStatusEqualTo(DataStatus.Valid.name());
        return biSkuSaleMapper.countByExample(example);
    }

    @Override
    public BiSkuSale findById(Long id) {
        return biSkuSaleMapper.selectByPrimaryKey(id);
    }

    @Override
    public int save(BiSkuSale biSkuSale) {
        return biSkuSaleMapper.insertSelective(biSkuSale);
    }

    @Override
    public int update(BiSkuSale biSkuSale) {
        return biSkuSaleMapper.updateByPrimaryKeySelective(biSkuSale);
    }

    @Override
    public ResultSet<BiSkuSale> search(QueryInfo queryInfo, String q) {
        List<BiSkuSale> list = biSkuSaleMapper.selectSuper(queryInfo, q);
        int count = biSkuSaleMapper.countSuper(q);
        ResultSet<BiSkuSale> resultSet = new ResultSet<BiSkuSale>(count, list);
        return resultSet;
    }

    @Override
    public int deleteSoft(Long[] ids) {
        Assert.notEmpty(ids);
        BiSkuSaleExample example = new BiSkuSaleExample();
        example.createCriteria().andIdIn(Arrays.asList(ids));
        BiSkuSale record = new BiSkuSale();
        record.setStatus(DataStatus.Delete);

        return biSkuSaleMapper.updateByExampleSelective(record, example);
    }

    @Override
    public int deleteSoft(Long id) {
        return this.deleteSoft(new Long[]{id});
    }

    @Override
    public int saveOrUpdate(BiSkuSale biSkuSale) {
        int r = 0;
        if (biSkuSale.getId() != null) {
            r = this.update(biSkuSale);
        } else {
            r = this.save(biSkuSale);
        }
        return r;
    }

    @Override
    public List<BiSkuSale> statisticalSkuSaleCount(QueryInfo queryInfo, Date startDate, Date endDate, String shopNo,
                                                   String productCode) {
        Assert.notNull(startDate);
        Assert.notNull(endDate);
        // startDate = DateConvertUtils.getDateStart(startDate);
        // endDate = DateConvertUtils.getDateStart(endDate);
        List<BiSkuSale> list = wdtQybOrderService.statisticalSkuSaleCount(queryInfo, startDate, endDate, shopNo,
                productCode);

        return list;
    }

    @Override
    public int addBatch(List<BiSkuSale> list) {
        if (CollectionUtils.isEmpty(list)) {
            return 0;
        }
        ArrayList<BiSkuSale> addList = new ArrayList<BiSkuSale>();
        for (int i = 0; i < list.size(); i++) {
            addList.add(list.get(i));
            if (i % 200 == 0 || i == list.size() - 1) {
                biSkuSaleMapper.addBatch(addList);
                addList.clear();
            }
        }
        return 1;
    }

    @Override
    public int countBiProductSaleList(Date startDate, Date endDate, String shopNo, String shopName, String productCode,
                                      String productName, Date salesStartDate, Date salesEndDate) {
        Assert.notNull(startDate);

        return biSkuSaleMapper.countBiProductSaleList(startDate, endDate, shopNo, shopName, productCode, productName,
                salesStartDate, salesEndDate);
    }

    @Override
    public ResultSet<BiSkuSale> searchBiProductSaleList(QueryInfo queryInfo, Date startDate, Date endDate,
                                                        String shopNo, String shopName, String productCode, String productName, Date salesStartDate,
                                                        Date salesEndDate) {


        List<BiSkuSale> list = biSkuSaleMapper.selectBiProductSaleList(queryInfo, startDate, endDate, shopNo, shopName, productCode,
                productName, salesStartDate, salesEndDate);

        int count = this.countBiProductSaleList(startDate, endDate, shopNo, shopName, productCode, productName,
                salesStartDate, salesEndDate);
        return new ResultSet<>(count, list);
    }

    @Override
    public List<BiSkuSale> searchBiProductByShop(Date startDate, Date endDate, String shopNo, String productCode) {
//		if (DateConvertUtils.isToday(startDate)) {
//			return biSkuSaleMapper.selectBiProductByShopCurr(productCode, shopNo);
//		} else {
        return biSkuSaleMapper.selectBiProductByShop(startDate, endDate, shopNo, productCode);
//		}
    }

    @Override
    public List<BiSkuSale> searchBiProductBySku(Date startDate, Date endDate, String shopNo, String... productCodes) {

        // if (DateConvertUtils.isToday(startDate)) {
        // return biSkuSaleMapper.selectBiProductBySkuCurr(shopNo,
        // productCodes);
        // } else {
        return biSkuSaleMapper.selectBiProductBySku(startDate, endDate, productCodes);
        // }
    }

    @Override
    public List<BiSkuSale> statisticalSkuSaleCountOnlyM(QueryInfo queryInfo, Date startDate, Date endDate,
                                                        String productCode) {
        Assert.notNull(startDate);
        Assert.notNull(endDate);
        startDate = DateConvertUtils.getDateStart(startDate);
        endDate = DateConvertUtils.getDateStart(endDate);
        return biSkuSaleMapper.statisticalSkuSaleCountOnlyM(queryInfo, startDate, endDate, productCode);
    }

    @Override
    public List<BiSkuSale> findSkuSaleList(Date biDate, String shopName) {
        biDate = DateConvertUtils.getDateStart(biDate);
        return biSkuSaleMapper.selectSkuSaleList(biDate, shopName);
    }

    @Override
    public int updateAmountBatch(List<BiSkuSale> list) {
        if (CollectionUtils.isEmpty(list)) {
            return 0;
        }
        ArrayList<BiSkuSale> updateList = new ArrayList<BiSkuSale>();
        for (int i = 0; i < list.size(); i++) {
            updateList.add(list.get(i));
            if (i % 200 == 0 || i == list.size() - 1) {
                biSkuSaleMapper.updateAmountBatch(updateList);
                updateList.clear();
            }
        }
        return 1;
    }

    @Override
    public int additionAmountBatch(List<BiSkuSale> list) {
        if (CollectionUtils.isEmpty(list)) {
            return 0;
        }
        ArrayList<BiSkuSale> updateList = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            updateList.add(list.get(i));
            if (i % 200 == 0 || i == list.size() - 1) {
                biSkuSaleMapper.additionAmountBatch(updateList);
                updateList.clear();
            }
        }
        return 1;
    }

    @Override
    public Boolean existed(String shopNo, String skuCode, Date biDate) {
        return biSkuSaleMapper.existed(shopNo, skuCode, biDate);

    }

    @Override
    public ResultSet<YgHotProductModel> searchHotProduct(QueryInfo queryInfo, Date startDate, Date endDate,String shopNo, String shopName, String productName) {

        int i = biSkuSaleMapper.countHotProduct(queryInfo, startDate, endDate,shopNo, shopName, productName);
        List<YgHotProductModel> list = biSkuSaleMapper.selectHotProductList(queryInfo, startDate, endDate,shopNo, shopName, productName);
        return new ResultSet<>(i, list);
    }
}
